uniform mat4 u_Matrix; // MVP
uniform mat4 u_MVMatrix; // MV
uniform vec3 u_LightPos; // The position of the light in eye space.

attribute vec4 a_Position;
attribute vec2 a_TextureCoordinates;
attribute vec3 a_Normal;

varying vec2 v_TextureCoordinates;
varying vec4 v_Color; // This will be passed into the fragment shader.

void main() {
    /*
    //Step one
    light vector = light position - object position
    cosine = dot product(object normal, normalize(light vector))
    lambert factor = max(cosine, 0)

    //Step two
    luminosity = 1 / (distance * distance)

    //Step three
    final color = material color * (light color * lambert factor * luminosity)
    */

    vec3 modelViewVertex = vec3(u_MVMatrix * a_Position);
    vec3 modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
    float distance = length(u_LightPos - modelViewVertex);
    vec3 lightVector = normalize(u_LightPos - modelViewVertex);
    float diffuse = max(dot(modelViewNormal, lightVector), 0.1);
    diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));
    v_Color = vec4(diffuse, diffuse, diffuse, 1.0);

    gl_Position = u_Matrix * a_Position;
    v_TextureCoordinates = a_TextureCoordinates;
}
